package com.liuyang.demo.redis;

import com.liuyang.demo.redis.cache.RedissonUtil;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * @author libin
 * @date 2015-07-07
 */
public class BaseRedissonConfig {

    @Value("${spring.redis.database:0}")
    protected Integer database;
    @Value("${spring.redis.host}")
    protected String host;
    @Value("${spring.redis.port}")
    protected Integer port;
    @Value("${spring.redis.password:}")
    protected String password;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        String redisHost = "redis://" + host + ":" + port;
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress(redisHost).setDatabase(database).setConnectionMinimumIdleSize(0);
        if (StringUtils.isNotBlank(password)) {
            singleServerConfig.setPassword(password);
        }
        return Redisson.create(config);
    }

    @Bean
    public RedissonUtil redissonUtil() {
        return new RedissonUtil();
    }
}
